package com.lhuc.controller;


import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.lhuc.common.core.domain.JsonResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

;

@RestController
public class HelloController {


    @GetMapping("/getPosition")
    public JsonResult getPosition(String address) throws Exception {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("address", address);
        paramMap.put("key", "18084ea29b47d1cf9ceacf619085e42d");
        String response = HttpUtil.get("https://restapi.amap.com/v3/geocode/geo", paramMap);
        JSONArray geo = JSONUtil.parseObj(response).getJSONArray("geocodes");
        String data = geo.get(0).toString();
        System.err.println(response);
        JSONObject jsonObject = JSONUtil.parseObj(data);
        String[] locations = jsonObject.get("location").toString().split(",");
        Map<String,Object> map = new HashMap<>();
        map.put("longitude", locations[0]);
        map.put("latitude", locations[1]);

        return JsonResult.getJsonResult(200,"查询成功",map);
    }

}
